﻿using System.ComponentModel;
using System.Runtime.CompilerServices;
using ServeAnything.Annotations;

namespace ServeAnything
{
    public class ViewModel : INotifyPropertyChanged
    {
        private string _serviceName;
        private string _path;
        private string _status;
        private string _username;
        private string _password;
        private bool _runAsSystem = true;

        public string ServiceName
        {
            get { return _serviceName; }
            set
            {
                if (value == _serviceName) return;
                _serviceName = value;
                OnPropertyChanged();
            }
        }

        public string Path
        {
            get { return _path; }
            set
            {
                if (value == _path) return;
                _path = value;
                OnPropertyChanged();

            }
        }

        public string Arguments { get; set; }

        public bool RunAsSystem
        {
            get { return _runAsSystem; }
            set
            {
                if (value.Equals(_runAsSystem)) return;
                _runAsSystem = value;
                OnPropertyChanged();
            }
        }

        public string Username
        {
            get { return _username; }
            set
            {
                if (value == _username) return;
                _username = value;
                FormatUsername();
                OnPropertyChanged();
            }
        }

        private void FormatUsername()
        {
            if (!_username.Contains("\\") && !_username.Contains("@") && !_username.StartsWith("\\."))
                _username = string.Format(".\\{0}", _username);
        }

        public string Password
        {
            get { return _password; }
            set
            {
                if (value == _password) return;
                _password = value;
                OnPropertyChanged();
            }
        }

        public string Status
        {
            get { return _status; }
            set
            {
                if (value == _status) return;
                _status = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
